//
//  SettingViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/2.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "SettingViewController.h"
#import "SetNameViewController.h"
#import "MyInfoTableViewCell.h"
#import "QRCodeViewController.h"
#import "FamiliarityNumberViewController.h"
#import "MemberManagementViewController.h"
#import "FaceRegisterViewController.h"
#import "GetDeviceInfoApi.h"

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *dataSource;
@property(nonatomic,strong) UIButton *switchButton;
@property(nonatomic,copy) NSString *deviceName;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getDeviceName];
}

#pragma mark --- 点击事件
- (void)didClickSwitchButton
{
    self.switchButton.selected = !self.switchButton.selected;
}

- (void)getDeviceName
{
    GetDeviceInfoApi *api = [[GetDeviceInfoApi alloc] initWithDic:@{@"device_id":EXRACT_OBJECT(DEVICEID)}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        self.deviceName = [request responseJSONObject][@"data"][@"name"];
        [self.tableView reloadData];
    } failure:nil];
}

#pragma mark --- tabelview代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return px1920Height(30);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    MyInfoTableViewCell *cell = [MyInfoTableViewCell dequeueReusableWithTableView:tableView];
    cell.infoLabel.hidden = YES;
    if (indexPath.section == 0)
    {
        cell.rightImageView.hidden = YES;
        __weak UIView *superView = cell.contentView;
        [superView addSubview:self.switchButton];
        [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(superView.mas_centerY);
            make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        }];
    }
    else if (indexPath.section == 2)
    {
        cell.infoLabel.hidden = NO;
        cell.infoLabel.text = self.deviceName;
    }
    cell.titleLabel.text = self.dataSource[indexPath.section];
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    switch (indexPath.section)
    {
        case 0:
        {
            [self didClickSwitchButton];
        }
            break;
        case 1:
        {
            FaceRegisterViewController *vc = [FaceRegisterViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            SetNameViewController *vc = [SetNameViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            FamiliarityNumberViewController *vc = [FamiliarityNumberViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            MemberManagementViewController *vc = [MemberManagementViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5:
        {
            QRCodeViewController *vc = [QRCodeViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark --- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        [superView addSubview:_tableView];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

- (NSArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = @[@"防盗设置",@"人脸识别注册",@"修改名称",@"亲情号码",@"成员管理",@"WiFi二维码"];
    }
    return _dataSource;
}

- (UIButton *)switchButton
{
    if(!_switchButton)
    {
        _switchButton = [[UIButton alloc]init];
        [_switchButton setImage:imageWithName(@"but_off") forState:UIControlStateNormal];
        [_switchButton setImage:imageWithName(@"but_on") forState:UIControlStateSelected];
        [_switchButton addTarget:self action:@selector(didClickSwitchButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}





#pragma mark --- 设置视图布局
- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(252));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.bottom.mas_equalTo(superView);
    }];
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"设置"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
